#include <iostream>
using namespace std;


/**
 * 小黑当站长3，删除节点
 */
int main() {
    int train[5] = {0, 10, 20, 30, 40};
    int next[5] = {-1, -1, -1, -1, -1};
    int head;
    cin >> head;
    int cur = head;
    for (int i = 1; i <= 3; i++) {
        int ne;
        cin >> ne;
        next[cur] = ne;
        cur = ne;
    }
    int del;
    cin >> del;

    if(head==del){
        head = next[head];
        next[del]=-1;
    }else{
        int cur = head;
        while(next[cur] != del && cur != -1){
            cur = next[cur];
        }
        if(cur != -1){
            next[cur] = next[del];
            next[del]=-1;
        }
    }
    
    while(head != -1) {
        cout << train[head] << " ";
        head = next[head];
    }
    return 0;
}